In [1]:
nums = [1, 2, 3, 4, 5]
s = sum(x * x for x in nums)
In [45]:
def dedupe(items, key=None):
seen = set()
for item in items:
val = item if key is None else key(item)
if val not in seen:
yield item
seen.add(val)
a = [ {'x':1, 'y':2}, {'x':1, 'y':3}, {'x':1, 'y':2}, {'x':2, 'y':4}]
nu = dedupe(a, key=lambda d: (d['x'],d['y']))
list(nu)
Out[45]:
In [46]:
from collections import deque
class linehistory:
def __init__(self, lines, histlen=3):
self.lines = lines
self.history = deque(maxlen=histlen)
def __iter__(self):
for line_index, line in enumerate(self.lines, 1):
self.history.append((line_index, line))
yield line
def clear(self):
self.history.clear()
with open('D:\\sql\\test.txt') as f:
lines = linehistory(f)
print(type(lines))
for line in lines:
if '# -' in line:
for lineno, hline in lines.history:
print('{}:{}'.format(lineno, hline), end='')
In [106]:
def frange(start, stop, increment):
x = start
while x < stop:
yield from add_run(x)
x += increment
def add_run(x):
yield x+x
f = iter(frange(1,9,1))
In [108]:
from itertools import islice
[x for x in islice(f, 1)]
Out[108]:
In [102]:
# fron logging
class Node(object):
def __init__(self, value):
self._value = value
self._children = []
def __repr__(self):
return 'Node({!r})'.format(self._value)
def add_child(self, node):
self._children.append(node)
def __iter__(self):
return iter(self._children)
def depth_first(self):
yield self
for s in self:
yield from s.depth_first()
In [103]:
root = Node(0)
child_1 = Node(1)
child_2 =Node(2)
root.add_child(child_1)
root.add_child(child_2)
# iter(root)
for c in root.depth_first():
print(c)
In [104]:
import logging as Log
Log.basicConfig(format='%(asctime)s %(message)s', level=Log.INFO)
class Cont(object):
def __init__(self, start, end):
self._start = start
self._end = end
def __iter__(self):
self.center = self._start
# Log.info('{}'.format(self._start))
# while n < self._end:
# yield n
# n += 1
return self
def __next__(self):
if self.center == self._end:
raise StopIteration
self.center += 1
return self.center
def __reversed__(self):
n = self._end
while n >= self._start:
yield n
n -= 1
In [105]:
for rr in Cont(3,9):
Log.info('info: {}'.format(rr))
In [170]:
class squares:
def __init__(self, start, stop):
self.flag = start - 1
self.value = self.flag
self.stop = stop
def __iter__(self):
# self.value = self.flag
return self
def __next__(self):
if self.value == self.stop:
raise StopIteration
self.value += 1
return self.value
a = squares(1,5)
b = squares(1,5)
# s = 0
# while s<=41:
# for i in a:
# s= s + i
# print(s)
In [4]:
from collections import Iterable
def flatten(items, ignore_types=(str, bytes)):
for x in items:
if isinstance(x, Iterable) and not isinstance(x, ignore_types):
yield from flatten(x)
else:
yield x
items = [1, 2, 8]
# Produces 1 2 3 4 5 6 7 8
for x in flatten(items):
print(x)
In [4]:
from collections import deque
class LineHistory:
def __init__(self, lines, histlen=1):
self.lines = lines
self.history = deque(maxlen=histlen)
def __iter__(self):
for lineno, line in enumerate(self.lines, 1):
self.history.append((lineno, line))
# 记录每次的
yield line
def clear(self):
self.history.clear()
with open('D:\\sql\\test.txt') as f:
line_history = LineHistory(f)
for line in line_history:
if 'SELECT' in line:
for lineno, hline in line_history.history:
print('{}:{}'.format(lineno, hline), end='')
In [9]:
def gg(a,b):
a, b=a, b
n = 0
while n<end:
a, b= b, a+b
yield a
n += 1
[x for x in gg(10)]
def sanjiao(end):
yield from
Out[9]:
In [ ]: